

import 'package:erp/Entity/WareHouse.dart';
import 'package:erp/Events/EventAddrs.dart';
import 'package:erp/Store/WareHouse.dart';
import 'package:erp/UI/CodeResources/Localization.dart';
import 'package:erp/UI/Components/EmbeddableStatefulWidget.dart';
import 'package:erp/UI/Components/EventfulState.dart';
import 'package:erp/UI/Components/ListDividerGenerator.dart';
import 'package:erp/UI/Components/LogoScaffold.dart';
import 'package:erp/UI/Components/MessageDialog.dart';
import 'package:erp/UI/Components/QuestionDialog.dart';
import 'package:erp/global.dart';
import "package:flutter/material.dart";
import 'package:erp/UI/Pages/BasicInformationManagementPages/WareHousePages/WareHouseEditDialog.dart';

class WareHousePageStateless extends StatelessWidget {
  bool selectionMode;

  WareHousePageStateless({this.selectionMode = false});

  @override
  Widget build(BuildContext context) {
    return EmbeddedWidget(
      initEvent: (widget){
        widget.eventIds = [
          widget.bus.subscribe(Events.WAREHOUSE_SELECT_SUCCESS , (bus, data){widget.setState((){});}),
          widget.bus.subscribe(Events.WAREHOUSE_INSERT_SUCCESS , (bus, data){widget.setState((){});}),
          widget.bus.subscribe(Events.WAREHOUSE_UPDATE_SUCCESS , (bus, data){widget.setState((){});}),
          widget.bus.subscribe(Events.WAREHOUSE_DELETE_SUCCESS , (bus, data){widget.setState((){});}),
          widget.bus.subscribe(Events.WAREHOUSE_DELETE_ERROR, (bus, error){
            showMessageDialog(widget.context, title: "ئۆچۈرۈش مەغلۇپ بولدى", content: error.toString());
          }),
        ];
      },
      widgetBuilder: (context, state){
        var dividerGenerator = ListDividerGenerator(WareHouseStore.wareHouses.map((item) => buildItem(context, item)).toList(), Divider());
        return LogoScaffold(
          title: "ئامبار باشقۇرۇش",
          assetsLogoName: "assets/pictures/warehause.png",
          heroName: "wareHouseImage",
          contentDelegate: SliverChildListDelegate(dividerGenerator.items),
          floatingActionButton: FloatingActionButton(
              child: Icon(Icons.add),
              onPressed: () async {
                await showWareHauseEditDialog(context, WareHouse());
              }
          ),
          useSearchBar: true,
        );
      }
    );
  }

  Widget buildItem(BuildContext context, WareHouse data){
    return ListTile(
      leading: Image.asset("assets/pictures/warehause.png", width: 48.0, height: 48.0,),
      title: Text(data.name),
      subtitle: Text(data.memo),
      trailing: SizedBox(
        width: 100.0,
          child: Row(
            children: <Widget>[
              IconButton(icon: Icon(Icons.edit), onPressed: () async {
                var cloned = data.clone();
                await showWareHauseEditDialog(context, data, false);
              }),
              IconButton(icon: Icon(Icons.delete), onPressed: () async {
                var result = await showQuestionDialog(context, title: "ئەسكەرتىش", content: "راستىنلا ئۆچۈرەمسىز؟") ?? false;
                if (result) globalBus.publish(Events.WAREHOUSE_DELETE_REQUEST, data);
              }),
            ],
          )
      ),
      onTap: (){
        if (selectionMode){
          Navigator.of(context).pop(data);
        }
      },
//    trailing: IconButton(icon: Icon(Icons.delete), onPressed: (){}),
    );
  }
}